const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  console.log('👤 获取用户注册状态')
  
  try {
    const { OPENID } = cloud.getWXContext()
    
    if (!OPENID) {
      return {
        success: false,
        error: '用户身份验证失败'
      }
    }
    
    // 查询用户的注册记录
    const result = await db.collection('construction_teams')
      .where({
        openid: OPENID
      })
      .get()
    
    if (result.data.length === 0) {
      return {
        success: true,
        data: {
          hasRegistration: false,
          status: null,
          canRegister: true,
          canModify: false
        }
      }
    }
    
    const registration = result.data[0]
    const auditStatus = registration.auditStatus
    
    // 根据审核状态确定用户可以进行的操作
    let canRegister = false
    let canModify = false
    let statusText = ''
    let statusColor = ''
    
    switch (auditStatus) {
      case 'pending':
        statusText = '审核中'
        statusColor = 'orange'
        canRegister = false
        canModify = false
        break
      case 'approved':
        statusText = '已通过'
        statusColor = 'green'
        canRegister = false
        canModify = false
        break
      case 'rejected':
        statusText = '已拒绝'
        statusColor = 'red'
        canRegister = false
        canModify = true // 被拒绝后可以修改重新提交
        break
      default:
        statusText = '未知状态'
        statusColor = 'gray'
        canRegister = true
        canModify = false
    }
    
    return {
      success: true,
      data: {
        hasRegistration: true,
        registration: {
          _id: registration._id,
          name: registration.name,
          type: registration.type,
          typeName: registration.typeName,
          phone: registration.phone,
          location: registration.location,
          category: registration.category,
          categoryName: registration.categoryName,
          experience: registration.experience,
          description: registration.description,
          specialties: registration.specialties || [],
          certifications: registration.certifications || [],
          avatar: registration.avatar,
          auditStatus: registration.auditStatus,
          auditTime: registration.auditTime,
          auditReason: registration.auditReason,
          submitTime: registration.submitTime,
          serviceRegions: registration.serviceRegions || [],
          // 个人师傅特有字段
          realName: registration.realName,
          idCard: registration.idCard,
          // 公司特有字段
          companyName: registration.companyName,
          businessLicense: registration.businessLicense,
          legalRepresentative: registration.legalRepresentative,
          registeredCapital: registration.registeredCapital,
          companyAddress: registration.companyAddress
        },
        status: auditStatus,
        statusText: statusText,
        statusColor: statusColor,
        canRegister: canRegister,
        canModify: canModify
      }
    }
    
  } catch (error) {
    console.error('❌ 获取用户注册状态失败:', error)
    return {
      success: false,
      error: error.message || '获取注册状态失败'
    }
  }
}
